'use strict';

/**
 * Config for the router
 */
angular.module('app')
  .run(
    [          '$rootScope', '$state', '$stateParams',
      function ($rootScope,   $state,   $stateParams) {
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
      }
    ]
  )
  .config(
    [          '$stateProvider', '$urlRouterProvider',
      function ($stateProvider,   $urlRouterProvider) {

          $urlRouterProvider
              .otherwise('/app/home');
          $stateProvider
	          .state('access', {
	              url: '/access',
	              template: '<div ui-view class="fade-in-right-big smooth"></div>'
	          })
	          .state('access.denied', {
	              url: '/denied',
	              templateUrl: 'tpl-common/denied/denied.html'
	          })
	          .state('access.login', {
	              url: '/login',
	              templateUrl: 'tpl-common/login/login.html',
	              resolve: {
	                  deps: ['uiLoad',
	                      function( uiLoad ){
	                          return uiLoad.load( ['tpl-common/login/login.js'] );
	                      }]
	              }
	          })
              .state('app', {
                  abstract: true,
                  url: '/app',
                  templateUrl: 'tpl/app.html'
              })
              // 系统管理部分
              .state('app.home', {
                  url: '/home',
                  templateUrl: 'home.html'
              })
              .state('app.system', {
                  url: '/system',
                  template: '<div ui-view></div>'
              })
              // 日志部分
              .state('app.system.log', {
                  url: '/log',
                  template: '<div ui-view></div>'
              })
              .state('app.system.log.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/log/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/log/list.js'
                              ]);
                          }]
                  }
              })
              // 数据字典部分
              .state('app.system.dictionary', {
                  url: '/dictionary',
                  template: '<div ui-view></div>'
              })
              .state('app.system.dictionary.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/dictionary/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/dictionary/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.dictionary.view', {
                  url: '/view/:id/:type',
                  templateUrl: 'system-tpl/dictionary/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/dictionary/view.js'
                              ]);
                          }]
                  }
              })
              // 系统配置部分
              .state('app.system.config', {
                  url: '/config',
                  template: '<div ui-view></div>'
              })
              .state('app.system.config.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/config/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/config/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.config.view', {
                  url: '/view/:id/:type',
                  templateUrl: 'system-tpl/config/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/config/view.js'
                              ]);
                          }]
                  }
              })
              // 权限部分
              .state('app.system.authority', {
                  url: '/authority',
                  template: '<div ui-view></div>'
              })
              .state('app.system.authority.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/authority/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/authority/list.js'
                              ]);
                          }]
                  }
              })
              // 角色部分
              .state('app.system.role', {
                  url: '/role',
                  template: '<div ui-view></div>'
              })
              .state('app.system.role.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/role/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/role/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.role.view', {
                  url: '/view/:id/:type/:isAdmin',
                  templateUrl: 'system-tpl/role/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/role/view.js'
                              ]);
                          }]
                  }
              })

              // 组部分
              .state('app.system.group', {
                  url: '/group',
                  template: '<div ui-view></div>'
              })
              .state('app.system.group.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/group/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/group/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.group.view', {
                  url: '/view/:id/:type/:parentId',
                  templateUrl: 'system-tpl/group/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['treeView']).then(
                                  function(){
                                      return $ocLazyLoad.load(['system-tpl/group/view.js']);
                                  }
                              );
                          }]
                  }
              })

              // 区域部分
              .state('app.system.region', {
                  url: '/region',
                  template: '<div ui-view></div>'
              })
              .state('app.system.region.list', {
                  url: '/list',
                  templateUrl: 'system-tpl/region/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/region/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.region.view', {
                  url: '/view/:id/:type/:parentId',
                  templateUrl: 'system-tpl/region/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['treeView']).then(
                                  function(){
                                      return $ocLazyLoad.load(['system-tpl/region/view.js']);
                                  }
                              );
                          }]

                  }
              })

              // 用户部分
              .state('app.self', {
                  url: '/self/',
                  templateUrl: 'system-tpl/user/updateSelf.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['ngImgCrop']).then(
                                  function(){
                                      return $ocLazyLoad.load(['system-tpl/user/updateSelf.js','js/controllers/imgcrop.js']);
                                  }
                              );
                          }]
                  }
              })
              // 用户部分
              .state('app.system.user', {
                  url: '/user',
                  template: '<div ui-view></div>'
              })
              .state('app.system.user.list', {
                  url: '/list/:isAdmin',
                  templateUrl: 'system-tpl/user/list.html',
                  params : {
                      isAdmin : 'isUser'
                  },
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'system-tpl/user/list.js'
                              ]);
                          }]
                  }
              })
              .state('app.system.user.view', {
                  url: '/view/:id/:type/:isAdmin',
                  templateUrl: 'system-tpl/user/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['ngImgCrop','treeView']).then(
                                  function(){
                                      return $ocLazyLoad.load(['system-tpl/user/view.js','js/controllers/imgcrop.js']);
                                  }
                              );
                          }]
                  }
              })
               .state('app.bpm', {
	              url: '/bpm',
	              template: '<div ui-view class="fade-in-right-big smooth"></div>'
	          })
	          .state('app.bpm.manager', {
	              url: '/manager',
	              template: '<div ui-view class="fade-in-right-big smooth"></div>'
	          })
	          .state('app.bpm.manager.definition', {
	        	  url: '/definition',
	        	  templateUrl: 'tpl/bpm/manager/processDefinition/definition.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['dialogs.main','dDatepicker']).then(function(){
                                  return $ocLazyLoad.load(['tpl/bpm/manager/processDefinition/definition.js']);
                              });
                          }]
                  }
	          })
	          .state('app.bpm.manager.deploy', {
	        	  url: '/deploy',
	        	  templateUrl: 'tpl/bpm/manager/processDefinition/deploy.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['angularFileUpload']).then(
                                  function(){
                                      return $ocLazyLoad.load(['tpl/bpm/manager/processDefinition/deploy.js']);
                                  }
                              );
                          }]
                  }
	          })
              .state('app.bpm.manager.processinstance', {
                  url: '/processinstance',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.manager.processinstance.list', {
                  url: '/list',
                  templateUrl: 'tpl/bpm/manager/processinstance/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          //function( $ocLazyLoad ){
                          //    return $ocLazyLoad.load(['dialogs.main','dDatepicker']).then(function(){
                          //        return $ocLazyLoad.load(['tpl/bpm/manager/processDefinition/definition.js']);
                          //    });
                          //}]
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([
                                  'tpl/bpm/manager/processinstance/list.js','tpl-common/inputDialog.js'
                              ]);
                          }]
                  }
              })
              .state('app.bpm.manager.process', {
                  url: '/process',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.manager.process.trace', {
                  url: '/trace',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.manager.process.trace.view', {
                  url: '/view/:executionId',
                  templateUrl: 'tpl/bpm/manager/trace/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['dialogs.main']).then(function(){
                                  return $ocLazyLoad.load(['tpl/bpm/manager/trace/view.js']);
                              });
                          }]
                  }
              })
              .state('app.bpm.manager.processlist', {
                  url: '/processlist',
                  templateUrl: 'tpl/bpm/manager/process/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['tpl/bpm/manager/process/list.js']);
                          }]
                  }
              })
              .state('app.bpm.manager.processStart', {
                  url: '/processStart/:processDefinitionId',
                  templateUrl: 'tpl/bpm/manager/process/start.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['dDatepicker']).then(function(){
                                  return $ocLazyLoad.load(['tpl/bpm/manager/process/start.js']);
                              });
                          }]
                  }
              })
              .state('app.bpm.manager.execution', {
                  url: '/execution',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.manager.execution.list', {
                  url: '/list',
                  templateUrl: 'tpl/bpm/manager/execution/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['tpl/bpm/manager/execution/list.js']);
                          }]
                  }
              })
              .state('app.bpm.manager.task', {
                  url: '/task',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.manager.task.list', {
                  url: '/list',
                  templateUrl: 'tpl/bpm/manager/task/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load(['tpl/bpm/manager/task/list.js']);
                          }]
                  }
              })
              .state('app.bpm.history', {
                  url: '/history',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.history.finsh', {
                  url: '/finsh',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('app.bpm.history.finsh.list', {
                  url: '/list',
                  templateUrl: 'tpl/bpm/history/list.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([]).then(function(){
                                  return $ocLazyLoad.load(['tpl/bpm/history/list.js']);
                              });
                          }]
                  }
              })
              .state('app.bpm.history.finsh.view', {
                  url: '/view/:processInstanceId',
                  templateUrl: 'tpl/bpm/history/view.html',
                  resolve: {
                      deps: ['$ocLazyLoad',
                          function( $ocLazyLoad ){
                              return $ocLazyLoad.load([]).then(
                                  function(){
                                      return $ocLazyLoad.load(['tpl/bpm/history/view.js']);
                                  }
                              );
                          }]
                  }
              })
      }
    ]
  );